/**
 * 
 */
package com.aibili.contoller.api;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.aibili.contoller.BaseController;
import com.aibili.service.FileService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.daoshun.common.DsConstant;
import com.daoshun.exception.NullParameterException;

/**
 * @author QiuCH
 *
 */

public class ApiBaseContoller extends BaseController {

	HttpServletRequest nowRequest;

	@Resource
	private FileService fileService;

	protected String toJson(HashMap<String, Object> resultMap) {
		return JSON.toJSONStringWithDateFormat(resultMap, "yyyy-MM-dd HH:mm:ss",
				SerializerFeature.DisableCircularReferenceDetect);
	}

	protected String toSuccessJson(HashMap<String, Object> resultMap) {
		resultMap.putAll(DsConstant.SUCCESS_RESULT_MAP);
		return JSON.toJSONStringWithDateFormat(resultMap, "yyyy-MM-dd HH:mm:ss",
				SerializerFeature.DisableCircularReferenceDetect);
	}

	protected void setRequest(HttpServletRequest request) {
		nowRequest = request;
		try {
			nowRequest.setCharacterEncoding(ENCODE);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	protected String getStringParam(String key) {
		return nowRequest.getParameter(key);
	}

	protected String getVaildatedStringParam(String key) throws NullParameterException {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			throw new NullParameterException();
		}
		return value;
	}

	int getIntValue(String key, int defval) {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			return defval;
		} else {
			try {
				return Integer.parseInt(value);
			} catch (Exception e) {
				return defval;
			}
		}
	}

	int getVaildatedIntValue(String key, int defval) throws NullParameterException {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			throw new NullParameterException();
		} else {
			try {
				int num = Integer.parseInt(value);
				if (num > 0) {
					return num;
				} else {
					throw new NullParameterException();
				}
			} catch (Exception e) {
				throw new NullParameterException();
			}
		}
	}

	long getLongValue(String key, long defval) {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			return defval;
		} else {
			try {
				return Long.parseLong(value);
			} catch (Exception e) {
				return defval;
			}
		}
	}

	long getVaildatedLongValue(String key, long defval) throws NullParameterException {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			throw new NullParameterException();
		} else {
			try {
				long num = Long.parseLong(value);
				if (num > 0) {
					return num;
				} else {
					throw new NullParameterException();
				}
			} catch (Exception e) {
				throw new NullParameterException();
			}
		}
	}

	float getFloatValue(String key, float defval) {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			return defval;
		} else {
			try {
				return Float.parseFloat(value);
			} catch (Exception e) {
				return defval;
			}
		}
	}

	float getVaildatedLongValue(String key, float defval) throws NullParameterException {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			throw new NullParameterException();
		} else {
			try {
				float num = Float.parseFloat(value);
				if (num > 0) {
					return num;
				} else {
					throw new NullParameterException();
				}
			} catch (Exception e) {
				throw new NullParameterException();
			}
		}
	}

	double getFloatValue(String key, double defval) {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			return defval;
		} else {
			try {
				return Double.parseDouble(value);
			} catch (Exception e) {
				return defval;
			}
		}
	}

	double getVaildatedDoubleValue(String key, float defval) throws NullParameterException {
		String value = nowRequest.getParameter(key);
		if (value == null || value.length() == 0) {
			throw new NullParameterException();
		} else {
			try {
				double num = Double.parseDouble(value);
				if (num > 0) {
					return num;
				} else {
					throw new NullParameterException();
				}
			} catch (Exception e) {
				throw new NullParameterException();
			}
		}
	}

	/**
	 * 没有缩略图
	 * 
	 * @param request
	 * @return 保存后的文件路径
	 */
	String getnormalFilePath(HttpServletRequest request) {
		String filePath = "";
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 判断 request 是否有文件上传,即多部分请求
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			try {
				while (iter.hasNext()) {
					// 取得上传文件
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						filePath = fileService.saveFile(file);
					}
				}
			} catch (Exception e) {
				return filePath;
			}
		}
		return filePath;
	}

	/**
	 * 返回图片路径有缩略图
	 * 
	 * @param request
	 * @return 保存后的文件路径
	 */
	String getorcaFilePath(HttpServletRequest request) {
		String filePath = "";
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 判断 request 是否有文件上传,即多部分请求
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			try {
				while (iter.hasNext()) {
					// 取得上传文件
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						filePath = fileService.saveImage(file);
					}
				}
			} catch (Exception e) {
				return filePath;
			}
		}
		return filePath;
	}

	/**
	 * 返回图片id有缩略图
	 * 
	 * @param request
	 * @return 保存后的文件id
	 */
	long getFileId(HttpServletRequest request) {
		long fileid = 0l;
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 判断 request 是否有文件上传,即多部分请求
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			try {
				while (iter.hasNext()) {
					// 取得上传文件
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						fileid = fileService.saveMomentImage(file);
					}
				}
			} catch (Exception e) {
				return fileid;
			}
		}
		return fileid;
	}
}
